package com.mobi.cacti.rabbit.connectors;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

import android.util.Log;

public class NewsAndPromotionServerConnector extends AbstractServerConnector{

	public void loadNews() {
		
		String url = "http://www.trueinternet.co.th/rssfeed/news.xml";
		
		this.execute(url,null);
	}

	public void loadPromotion() {
		
		String url = "http://www.trueinternet.co.th/rsstimonitor/promotions.xml";
		
		this.execute(url,null);
	}
	
	@Override
	protected String doInBackground(String... params) {

		try 
		{
			URL url = new URL(params[0]);
			InputStream is = url.openStream();
			
			String result = convertStreamToString(is);
			return result;

		} catch (Exception e) {
//	    	Log.i("tai", "IOException");
			e.printStackTrace();
		}

		return null;
	}
	
	private String convertStreamToString(InputStream is) {
	    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
	    StringBuilder sb = new StringBuilder();

	    String line = null;
	    try {
	        while ((line = reader.readLine()) != null) {
	            sb.append(line + "\n");
	        }
	    } catch (IOException e) {
	        e.printStackTrace();
	    } finally {
	        try {
	            is.close();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	    }
	    return sb.toString();
	}

	
	

}
